﻿using System;
using System.Web.Mvc;
using System.Web.Http;

using MiniSched.Web.Abstractions;

namespace MiniSched.Web.Areas.Api {
    public class ApiAreaRegistration : AreaRegistration {

        internal GlobalConfigurationAbstraction GlobalConfiguration {
            get {
                if (null == _GlobalConfiguration) _GlobalConfiguration = GlobalConfigurationAbstraction.Default;
                return _GlobalConfiguration;
            }
            set {
                if (null == value) throw new ArgumentNullException("GlobalConfiguration");
                _GlobalConfiguration = value;
            }
        }
        private GlobalConfigurationAbstraction _GlobalConfiguration;

        public override string AreaName {
            get {
                return "Api";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) {
            var httpConfiguration = GlobalConfiguration.Configuration;
            if (httpConfiguration == null) throw new InvalidOperationException("The HTTP configuration cannot be null.");

            var routes = httpConfiguration.Routes;
            if (routes == null) throw new InvalidOperationException("The routes cannot be null.");

            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
